//
//  EMSSkillPlannerSkillNodeLayer.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 4/25/09.
//  Copyright 2009  __Vadim__. All rights reserved.
//

#import "EMSSkillPlannerSkillNodeLayer.h"
#import "EMSSkillTrainingInfo.h"
#import "EMSSkillPlannerSkillBranchView.h"
#import "EMSSkillPlannerTreeNode.h"
#import "EVECDCharacter.h"
#import "EVECDLearnedSkill.h"
#import "EVECDSkill.h"

@interface EMSSkillPlannerSkillNodeLayer ()

@property (retain) CATextLayer* titleTextLayer;
@property (retain) CATextLayer* currentLevelTextLayer;
@property (retain) CATextLayer* requiredLevelTextLayer;
@property (retain) CATextLayer* thisTimeTextLayer;
@property (retain) CATextLayer* prequesitesTimeTextLayer;

@end

@interface EMSSkillPlannerSkillNodeLayer (layersSetupMethods)

-(void)setupTitleTextLayer;
-(void)setupCurrentLevelTextLayer;
-(void)setupRequiredLevelTextLayer;
-(void)setupThisTimeTextLayer;
-(void)setupPrequisiteTimeTextLayer;

@end


@implementation EMSSkillPlannerSkillNodeLayer

@synthesize representativeNode=_representativeNode;
@synthesize parentNodeLayer=_parentNodeLayer;
@synthesize titleTextLayer=_titleTextLayer;
@synthesize currentLevelTextLayer=_currentLevelTextLayer;
@synthesize requiredLevelTextLayer=_requiredLevelTextLayer;
@synthesize thisTimeTextLayer=_thisTimeTextLayer;
@synthesize prequesitesTimeTextLayer=_prequesitesTimeTextLayer;

+(id)nodeLayerWithRepresentation:(EMSSkillPlannerTreeNode*)aNode
{
	EMSSkillPlannerSkillNodeLayer* contentLayer = [EMSSkillPlannerSkillNodeLayer layer];
	contentLayer.name = @"contentLayer";
	contentLayer.bounds = CGRectMake(0, 0, WIDTH, 70);
	contentLayer.representativeNode = aNode;
	contentLayer.borderWidth = 1.0;
	contentLayer.borderColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
	
	[contentLayer setupTitleTextLayer];
	[contentLayer setupCurrentLevelTextLayer];
	
	TrainingStatus status = aNode.trainingStatus;
	
	if(status & trainingStatusLearnedEnough)
	{
		if(!(status & trainingStatusRequestedSkill))
		{
			[contentLayer setupRequiredLevelTextLayer];
		}
		contentLayer.backgroundColor = CGColorCreateGenericRGB(0.0, 1.0, 0.0, 1.0);
	} 
	else if(status & trainingStatusRequestedSkill)
	{
		contentLayer.backgroundColor = CGColorCreateGenericRGB(0.2, 0.2, 1.0, 1.0);
		[contentLayer setupThisTimeTextLayer];
		if(!(status & trainingStatusLearnable))
		{
			[contentLayer setupPrequisiteTimeTextLayer];
		}
	} else {
		[contentLayer setupRequiredLevelTextLayer];
		[contentLayer setupThisTimeTextLayer];
		if(status & trainingStatusLearnable)
		{
			contentLayer.backgroundColor = CGColorCreateGenericRGB(1.0, 0.65, 0.0, 1.0);
		} else {
			contentLayer.backgroundColor = CGColorCreateGenericRGB(1.0, 0.2, 0.2, 1.0);
			[contentLayer setupPrequisiteTimeTextLayer];
		}
	}

	return contentLayer;
}

-(void)dealloc
{
	[self.representativeNode release];
	[self.titleTextLayer release];
	[self.currentLevelTextLayer release];
	if(self.requiredLevelTextLayer) [self.requiredLevelTextLayer release];
	if(self.thisTimeTextLayer) [self.thisTimeTextLayer release];
	if(self.prequesitesTimeTextLayer) [self.prequesitesTimeTextLayer release];
	[super dealloc];
}

#pragma mark Bindings Stuff

+(void)initialize
{
    [self exposeBinding:@"value"];
}

- (void)bind:(NSString*)bindingName
    toObject:(id)observableObject
 withKeyPath:(NSString*)observableKeyPath
     options:(NSDictionary*)options
{
    [observableObject addObserver:self
                       forKeyPath:observableKeyPath
                          options:(NSKeyValueObservingOptionNew)
                          context:nil];
}

-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
	[self updateThisTimeLayer];
}

-(void)unbind:(NSString*)bindingName
{
	[self.representativeNode.learnedSkill removeObserver:self forKeyPath:@"skillPointsLearned"];
}

-(void)updateThisTimeLayer
{
	NSValueTransformer*  valueTransformer = [NSValueTransformer valueTransformerForName:@"SecondsToCountdownStringValueTransformer"];
	NSTimeInterval time = self.representativeNode.thisTime;
	NSString* countdownString = [valueTransformer transformedValue:[NSNumber numberWithDouble:time]];
	self.thisTimeTextLayer.string = [NSString stringWithFormat:@"This Time: %@", countdownString];	
	if(self.parentNodeLayer)
	{
		[self.parentNodeLayer updatePrequisiteTimeTextLayer];
	}
}

-(void)updatePrequisiteTimeTextLayer
{
	NSValueTransformer*  valueTransformer = [NSValueTransformer valueTransformerForName:@"SecondsToCountdownStringValueTransformer"];
	NSTimeInterval time = self.representativeNode.prequisiteTime;
	NSString* countdownString = [valueTransformer transformedValue:[NSNumber numberWithDouble:time]];
	self.prequesitesTimeTextLayer.string = [NSString stringWithFormat:@"Prequisite Time: %@", countdownString];
	if(self.parentNodeLayer)
	{
		[self.parentNodeLayer updatePrequisiteTimeTextLayer];
	}
}

@end


@implementation EMSSkillPlannerSkillNodeLayer (layersSetupMethods)

-(void)setupTitleTextLayer
{
	CATextLayer* titleTextLayer = [CATextLayer layer];
	titleTextLayer.name = @"titleTextLayer";
	//titleTextLayer.font = [NSFont fontWithName:@"Monoco" size:10];
	titleTextLayer.font = [NSFont boldSystemFontOfSize:12.0];
	titleTextLayer.fontSize = 12.0;
	titleTextLayer.alignmentMode = kCAAlignmentCenter;
	titleTextLayer.bounds = CGRectMake(0, 0, WIDTH, 14);
	titleTextLayer.position = CGPointMake((WIDTH / 2), 60);
	titleTextLayer.foregroundColor = CGColorGetConstantColor(kCGColorBlack);
	
	self.titleTextLayer = titleTextLayer;
	titleTextLayer.string = self.representativeNode.learnedSkill.skill.name;
	[self addSublayer:titleTextLayer];
}

-(void)setupCurrentLevelTextLayer
{
	CATextLayer* currentLevelTextLayer = [CATextLayer layer];
	currentLevelTextLayer.name = @"currentLevelTextLayer";
	currentLevelTextLayer.font = [NSFont labelFontOfSize:10.0];
	currentLevelTextLayer.fontSize = 10.0;
	currentLevelTextLayer.alignmentMode = kCAAlignmentLeft;
	currentLevelTextLayer.bounds = CGRectMake(0, 0, (WIDTH - 10), 12);
	currentLevelTextLayer.position = CGPointMake((WIDTH / 2), 48);
	currentLevelTextLayer.foregroundColor = CGColorGetConstantColor(kCGColorBlack);
	
	NSString* currentLevelValueString;
	if(self.representativeNode.learnedSkill)
	{
		currentLevelValueString = [NSString stringWithFormat:@"%@", self.representativeNode.learnedSkill.level];
	} else 
	{
		currentLevelValueString = @"(none)";
	}
	
	self.currentLevelTextLayer = currentLevelTextLayer;
	currentLevelTextLayer.string = [NSString stringWithFormat:@"Current Level: %@", currentLevelValueString];
	[self addSublayer:currentLevelTextLayer];
}

-(void)setupRequiredLevelTextLayer
{
	CATextLayer* requiredLevelTextLayer = [CATextLayer layer];
	requiredLevelTextLayer.name = @"requiredLevelTextLayer";
	requiredLevelTextLayer.font = [NSFont labelFontOfSize:10.0];
	requiredLevelTextLayer.fontSize = 10.0;
	requiredLevelTextLayer.alignmentMode = kCAAlignmentLeft;
	requiredLevelTextLayer.bounds = CGRectMake(0, 0, (WIDTH - 10), 12);
	requiredLevelTextLayer.position = CGPointMake((WIDTH / 2), 36);
	requiredLevelTextLayer.foregroundColor = CGColorGetConstantColor(kCGColorBlack);
	
	self.requiredLevelTextLayer = requiredLevelTextLayer;
	requiredLevelTextLayer.string = [NSString stringWithFormat:@"Required Level: %i", self.representativeNode.level];
	[self addSublayer:requiredLevelTextLayer];
}

-(void)setupThisTimeTextLayer
{
	CATextLayer* thisTimeTextLayer = [CATextLayer layer];
	thisTimeTextLayer.name = @"thisTimeTextLayer";
	thisTimeTextLayer.font = [NSFont labelFontOfSize:10.0];
	thisTimeTextLayer.fontSize = 10.0;
	thisTimeTextLayer.alignmentMode = kCAAlignmentLeft;
	thisTimeTextLayer.bounds = CGRectMake(0, 0, (WIDTH - 10), 12);
	thisTimeTextLayer.position = CGPointMake((WIDTH / 2), 24);
	thisTimeTextLayer.foregroundColor = CGColorGetConstantColor(kCGColorBlack);
	self.thisTimeTextLayer = thisTimeTextLayer;
	[self updateThisTimeLayer];
	[self addSublayer:thisTimeTextLayer];
}

-(void)setupPrequisiteTimeTextLayer
{
	CATextLayer* prequesitesTimeTextLayer = [CATextLayer layer];
	prequesitesTimeTextLayer.name = @"prequesitesTimeTextLayer";
	prequesitesTimeTextLayer.font = [NSFont labelFontOfSize:10.0];
	prequesitesTimeTextLayer.fontSize = 10.0;
	prequesitesTimeTextLayer.alignmentMode = kCAAlignmentLeft;
	prequesitesTimeTextLayer.bounds = CGRectMake(0, 0, (WIDTH - 10), 12);
	prequesitesTimeTextLayer.position = CGPointMake((WIDTH / 2), 12);
	prequesitesTimeTextLayer.foregroundColor = CGColorGetConstantColor(kCGColorBlack);
	self.prequesitesTimeTextLayer = prequesitesTimeTextLayer;
	[self updatePrequisiteTimeTextLayer];
	[self addSublayer:prequesitesTimeTextLayer];
}

@end